package  com.baomidou.springwind.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.framework.controller.SuperController;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.common.HoorayosHelper;
import com.baomidou.springwind.entity.Member;
import com.baomidou.springwind.entity.Setting;

/**
 *  日历控制器首页
 * @author koumiba
 * @Date 2016-04-13
 */
@Controller
@RequestMapping("/sysapp/desksetting")
public class DesksettingController extends SuperController {
	private Map<String,String> errorCode;
			
		
	/**
	 * 主页
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/index")
	public String index(Model model) {
		initErrorCode();
		model.addAttribute("errorCode", errorCode);
		//验证是否登入
		if(!HoorayosHelper.checkLogin(request)){
			model.addAttribute("code", errorCode.get("noLogin"));
			return "error";
		}

		int mid = HoorayosHelper.getMemberId(request);
		Map<String, Object> settingMap = new HashMap<String, Object>();
		if(HoorayosHelper.checkLogin(request)){
			EntityWrapper ew = new EntityWrapper(new Member());
			ew.and("tbid="+mid);
			Member member = new Member().selectOne(ew);
			settingMap.put("desk", member.getDesk());
			settingMap.put("xy", member.getAppxy());
			settingMap.put("size", member.getAppsize());
			settingMap.put("pos", member.getDockpos());
			settingMap.put("vertical", member.getAppverticalspacing());
			settingMap.put("horizontal", member.getApphorizontalspacing());
			
		}else{
			EntityWrapper ew = new EntityWrapper(new Setting());
			Setting setting = new Setting().selectOne(ew);
			settingMap.put("desk", setting.getDesk());
			settingMap.put("xy", setting.getAppxy());
			settingMap.put("size", setting.getAppsize());
			settingMap.put("pos", setting.getDockpos());
			settingMap.put("vertical", setting.getAppverticalspacing());
			settingMap.put("horizontal", setting.getApphorizontalspacing());
		}
		model.addAttribute("settingMap", settingMap);
		
		return "/sysapp/desksetting/index";
	}
	
	/**
	 * ajax页面 TODO 待完善
	 */
	@ResponseBody
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/ajax")
	public String ajax() {
		String display="";
		return display;
	}
	/*************************
	 *    私有方法 
	 *************************/
	
	/**
	 * 验证是否为管理员
	 * @return Boolean
	 * koumiba
	 * 2017年3月26日
	 */
	private Boolean checkAdmin(){
		int mid = HoorayosHelper.getMemberId(request);
		EntityWrapper ew = new EntityWrapper(new Member());
		ew.setSqlSelect("type").where("where tbid="+mid);
		Member m = new Member().selectOne(ew);
		return m.getType() == 1 ? true : false;
	}
	/**
	 * 初始化ErrorCode
	 *  void
	 * koumiba
	 * 2017年3月27日
	 */
	private void initErrorCode(){
		errorCode = new HashMap<String,String>();
		errorCode.put("noLogin", "1000");
		errorCode.put("noAdmin", "1001");
		errorCode.put("noPermissions", "1002");
	}
	
}
